<?php

defined('__MAPEXEC__') or die('No direct access allowed');

/**
 * Base class for all map icon classes
 */
class Maps_Icon_Common {

  protected $anchorOffsetX = 0;
  protected $anchorOffsetY = 0;
  private $baseURL = '';
  private $iconHeight = -1;
  private $iconImage = '';
  private $iconWidth = -1;
  private $id = '';
  protected $infoWindowOffsetX = 0;
  protected $infoWindowOffsetY = 0;
  
  /**
   * Class constructor
   * @param   string  Optional icon image base URL
   * @return  void
   */
  public function __construct($baseURL='') {
    $this->setBaseURL($baseURL);
  }
  
  /**
   * Gets the horizontal pixel offset for where the icon should appear, relative to the upper
   * left corner of the image.
   * @param   void
   * @return  integer   Number of pixels
   */
  public function getAnchorOffsetX() {
    return $this->anchorOffsetX;
  }
  
  /**
   * Gets the vertical pixel offset for where the icon should appear, relative to the upper
   * left corner of the image.
   * @param   void
   * @return  integer   Number of pixels
   */
  public function getAnchorOffsetY() {
    return $this->anchorOffsetY;
  }
  
  /**
   * Gets the base URL for the images which make up this icon.
   * @param   void
   * @return  string  URL
   */
  public function getBaseURL() {
  	return $this->baseURL;
  }
  
  /**
   * Gets the icon height in pixels
   * @param   void
   * @return  integer   Icon height in pixels
   */
  public function getIconHeight() {
  	return $this->iconHeight;
  }
  
  /**
   * Gets the icon image path
   * @param   void
   * @return  string  Image path
   */
  public function getIconImage() {
    return $this->iconImage;
  }
  
  /**
   * Gets the full icon image URL
   * @param   void
   * @return  string  Icon image URL
   */
  public function getIconImageURL() {
    return $this->getImageURL( $this->getIconImage() );
  }
  
  /**
   * Gets the icon width in pixels
   * @param   void
   * @return  integer   Icon width
   */
  public function getIconWidth() {
  	return $this->iconWidth;
  }
  
  /**
   * Gets the Id for this icon.
   * @param   void
   * @return  string  Icon Id
   */
  public function getId() {
    return $this->id;
  }
  
  /**
   * Gets a full image URL
   * @param   string  Image path
   * @return  string  Full URL to an image
   */
  protected function getImageURL($image) {
    return $this->getBaseURL() . $image;
  }
  
  /**
   * Gets the horizontal offset of the info window's anchor point on the map.
   * @param   void
   * @return  integer   Horizontal offset in pixels
   */
  public function getInfoWindowOffsetX() {
    return $this->infoWindowOffsetX;
  }
  
  /**
   * Gets the vertical offset of the info window's anchor point on the map.
   * @param   void
   * @return  integer   Vertical offset in pixels
   */
  public function getInfoWindowOffsetY() {
    return $this->infoWindowOffsetY;
  }
  
  /**
   * Sets the image URL for this icon.
   * @param string $image Path to icon image
   * @return void
   */
  public function setBaseURL($str) {
    if ( is_string($str) && !empty($str) ) {
      $this->baseURL = $str;
    }
  }
  
  /**
   * Sets the icon information
   * @param   integer   Icon width
   * @param   integer   Icon height
   * @param   string    Icon image path
   * @return  void
   */
  public function setIcon($width, $height, $image) {
    $this->setIconWidth($width);
    $this->setIconHeight($height);
    $this->setIconImage($image);
    $this->updateOffsets();
  }
  
  /**
   * Sets the height of this icon.
   * @param integer $height Height in pixels
   * @return void
   */
  public function setIconHeight($height=-1) {
  	$this->iconHeight = round($height);
  }
  
  /**
   * Sets the icon image path
   * @param   string  Icon image path
   * @return  void
   */
  public function setIconImage($str='') {
    if (is_string($str) && $str != '') {
      $this->iconImage = $str;
    }
  }
  
  /**
   * Sets the width of this icon.
   * @param integer $width Width in pixels
   * @return void
   */
  public function setIconWidth($width=-1) {
    $this->iconWidth = round($width);
  }
  
  /**
   * Sets the icon id
   * @param   string  Icon Id
   * @return  void
   */
  public function setId($id) {
    if ( is_string($id) && !empty($id) ) {
      $this->id = $id;
    }
  }

  /**
   * Update the pixel offsets for this icon image, relative from the top, left corner
   * of the image, at which the image should be anchored to the map.
   * @param void
   * @return void
   */
  protected function updateAnchorOffsets() {
    $height = $this->getIconHeight();
    $width = $this->getIconWidth();
    if ($height <= 0 || $width <= 0) {
      return;
    }
    $this->anchorOffsetX = round($width / 2);
    $this->anchorOffsetY = round($height);
  }

  /**
   * Update the pixel offsets for this icon image's info window, from the top, left
   * corner of the image, at which the info window should be anchored to this icon.
   * @param void
   * @return void
   */
  protected function updateInfoWindowOffsets() {
  	$height = $this->getIconHeight();
    $width = $this->getIconWidth();
    if ($height <= 0 || $width <= 0) {
      return;
    }
    $this->infoWindowOffsetX = round($width / 2);
    $this->infoWindowOffsetY = 0;
  }
  
  /**
   * Updates the icon and info window offsets for this icon
   * @param   void
   * @return  void
   */
  private function updateOffsets() {
    $this->updateAnchorOffsets();
    $this->updateInfoWindowOffsets();
  }

}
?>